layui.use(['form','layer','laydate','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;

    //学生列表实例
    var tableIns = table.render({
        elem: '#newsList',
        url : '/teachers/selectall',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limit : 10,
        limits : [5,10,15,20],
        id : "newsListTable",/*此id缺失则从原始html中获取id*/
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'id', title: 'ID', width:60, align:"center",sort:true},
            {field: 'name', title: '姓名', width:90},
            {field: 'tnumber', title: '工号', align:'center',width:90},
            {field: 'sex', title: '性别',  align:'center',width:90,templet:function(d){
                return d.sex==0?"女":"男";
                }},
            {field: 'createtime', title: '创建时间', align:'center', width:160,minWidth:160, templet:function(d){
                var time=new Date(d.createtime);
                return time.toLocaleDateString()+ " "+time.toTimeString().slice(0,8)
            }},

            {title: '操作', width:300, templet:'#newsListBar',fixed:"right",align:"center"}
        ]]
    });



    //搜索
    $(".search_btn").on("click",function(){

            table.reload("newsListTable",{
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                   name: $(".searchVal").val(),
                   tnumber: $(".searchNumVal").val(),
                }
            })
    });
    function classList(data){
         classes=data.data.classes;
         var str='';
        for(var i=0;i<classes.length;i++){
            str=str+classes[i].cname+"   "+classes[i].level;
            str+='&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp'
        }
        layer.open({
            type: 1,
            skin: 'layui-layer-rim', //加上边框
            area: ['300px', '100px'], //宽高
            content: str,
            title: "授课班级"
        })
    }
    var arrSelect;
    //添加教师
    function addNews(edit){
        var index = layui.layer.open({
            title : "添加教师",
            type : 2,
            content : "/router/toteachersadd",
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                if(edit){
                    body.find(".name").val(edit.name);
                    body.find(".tnumber").val(edit.tnumber);
                    body.find(".id").val(edit.id);

                    body.find("input[name=sex]").each(function(){
                        if($(this).val()==edit.sex){
                            $(this).prop("checked", true);
                        }
                    })

                    /*edit.select是一个数组，存着班级id信息*/
                    arrSelect = edit.select;
                    var iframe = window['layui-layer-iframe' + index];
                    // 向子页面的全局函数child传参
                    iframe.child(arrSelect);
                    var time=new Date(edit.createtime);
                        time= time.toLocaleDateString()+ " "+time.toTimeString().slice(0,8)
                    body.find(".createtime").val(time);
                    form.render();
                }else {
                    // 获取子页面的iframe
                    var iframe = window['layui-layer-iframe' + index];
                    // 向子页面的全局函数child传参
                    arrSelect=null;
                    iframe.child(arrSelect);
                }

                setTimeout(function(){
                    layui.layer.tips('点击此处返回列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        })
        layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(index);
        })
    }
    $(".addNews_btn").click(function(){
        addNews();
    })

    //批量删除
    $(".delAll_btn").click(function(){
        var checkStatus = table.checkStatus('newsListTable'),
            data = checkStatus.data,
            ids = [];
            if(data.length > 0) {
            for (var i in data) {
                ids.push(data[i].id);
            }

            layer.confirm('确定删除选中的教师？', {icon: 3, title: '提示信息'}, function (index) {
                $.ajax({
                    type:"post",
                    url:"/teachers/deletedisp",
                    data:JSON.stringify(ids),
                    contentType:"application/json",
                    success: function (data) {
                        layer.msg(data.msg);
                        tableIns.reload({
                            done:function(res, curr, count){
                                if(res.data.length==0){
                                    curr=curr-1;
                                    tableIns.reload({
                                        page:{
                                            curr:curr
                                        }
                                    })
                                }

                            }
                        })
                        layer.close(index);
                    }
                })
            })
        }else{
            layer.msg("请选择需要删除的教师");
        }
    })

    //列表操作
    table.on('tool(newsList)', function(obj){
        var layEvent = obj.event,/*事件类型*/
            data = obj.data;/*拿到整行数据*/


        if(layEvent === 'edit'){ //编辑
            $.get("/teachers/classList",{
                id : data.id  //将需要查询的教师id传入
            },function(ss) {
                classes=ss.data.classes;
                select=[];
                for(var i in classes){
                    select.push(classes[i].id);
                }
                data.select=select;
                addNews(data);
            })


        }else if(layEvent === 'look'){
            $.get("/teachers/classList",{
                id : data.id  //将需要查询的教师id传入
            },function(data) {
                classList(data)

            })
        } else if(layEvent === 'del'){ //删除
            layer.confirm('确定删除此老师？',{icon:3, title:'提示信息'},function(index){
                $.get("/teachers/delete",{
                    id : data.id  //将需要删除的Id作为参数传入
                },function(data){
                    layer.msg(data.msg);
                    tableIns.reload({
                        done:function(res, curr, count){
                            if(res.data.length==0){
                                curr=curr-1;
                                tableIns.reload({
                                    page:{
                                        curr:curr
                                    }
                                    })
                            }

                        }
                    })
                    layer.close(index);
                })
            });
        }
    });

})